{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import joblib\n",
    "import warnings\n",
    "import os\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "use_cols = joblib.load('./use_cols')\n",
    "scaler = joblib.load('./scaler')\n",
    "pca = joblib.load('./model/pca_1XX')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_train(train_no=1, use_cols=None):\n",
    "    '''\n",
    "    描述：\n",
    "        提取trian_HA的特征，数据要从concat_cut中取。\n",
    "    '''\n",
    "    if use_cols==None:\n",
    "        use_cols = []\n",
    "        \n",
    "    input_dir = '../../2.1preprocess_train_data/concats_more/'\n",
    "    train = joblib.load(input_dir+'concat_0%d.lz4'%train_no) \n",
    "    y_train = train[['RULR']]\n",
    "    x_train = train[use_cols]\n",
    "    CLI = train['CLI']\n",
    "    CL = train['CL']\n",
    "    return x_train, y_train, CLI, CL\n",
    "\n",
    "def standard_scale_new(data,scaler):\n",
    "    cols = data.columns\n",
    "    idx = data.index\n",
    "    data_s = scaler.transform(data.values)\n",
    "    return pd.DataFrame(data=data_s,columns=cols,index=idx)\n",
    "\n",
    "def cal_T2(data,pca):\n",
    "    T2=[]\n",
    "    projected_data = pca.transform(data)\n",
    "    eigenvalue_matrix = np.mat(np.diag(pca.singular_values_ ))\n",
    "    for x in projected_data:\n",
    "        tmp = np.mat(x)*np.linalg.inv(eigenvalue_matrix)*np.mat(x).T\n",
    "        T2.append(tmp[0,0])\n",
    "    return T2\n",
    "\n",
    "def write_files(train_no, use_cols, scaler, pca):\n",
    "    x_train, y_train, CLI, CL = load_train(train_no, use_cols)\n",
    "    print(x_train.shape)\n",
    "    data_stand = standard_scale_new(x_train,scaler)\n",
    "    T2 = cal_T2(data_stand,pca)\n",
    "    \n",
    "    T2 = pd.DataFrame(data=T2,columns=['PCA_T2'], index=data_stand.index)\n",
    "    T2['CLI'] = CLI\n",
    "    T2['CL'] = CL\n",
    "    T2['RULR'] = y_train\n",
    "    \n",
    "    if not os.path.exists('./train_HA'):\n",
    "        os.mkdir('./train_HA')\n",
    "    joblib.dump(T2, './train_HA/train_HA_0%d.lz4'%train_no, compress='lz4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(110027, 17)\n"
     ]
    }
   ],
   "source": [
    "write_files(1, use_cols, scaler, pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(107839, 17)\n"
     ]
    }
   ],
   "source": [
    "write_files(2, use_cols, scaler, pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(85293, 17)\n"
     ]
    }
   ],
   "source": [
    "write_files(3, use_cols, scaler, pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PCA_T2</th>\n",
       "      <th>CLI</th>\n",
       "      <th>CL</th>\n",
       "      <th>RULR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30.296621</td>\n",
       "      <td>5</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>0.983333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31.557455</td>\n",
       "      <td>5</td>\n",
       "      <td>4.000167</td>\n",
       "      <td>0.983333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31.806602</td>\n",
       "      <td>5</td>\n",
       "      <td>4.000333</td>\n",
       "      <td>0.983332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>31.564906</td>\n",
       "      <td>5</td>\n",
       "      <td>4.000500</td>\n",
       "      <td>0.983331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>31.426296</td>\n",
       "      <td>5</td>\n",
       "      <td>4.000667</td>\n",
       "      <td>0.983331</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      PCA_T2  CLI        CL      RULR\n",
       "0  30.296621    5  4.000000  0.983333\n",
       "1  31.557455    5  4.000167  0.983333\n",
       "2  31.806602    5  4.000333  0.983332\n",
       "3  31.564906    5  4.000500  0.983331\n",
       "4  31.426296    5  4.000667  0.983331"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 健康指标 (PCA_T2)\n",
    "train_HA_01 = joblib.load('./train_HA/train_HA_01.lz4')\n",
    "train_HA_01.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
